const { text } = require('body-parser')
const express = require('express')
const jwt = require('jsonwebtoken')
const router = express.Router()
const db = require('../db')

module.exports = router

router.post('/reg', async (req, res) => {
    const { username, password } = req.body;
    // console.log(req.body);
    // console.log(username);
    // 数据库操作
    // res.send({
    //     code: 200,
    // })
    let sql1 = `select username from user where username='${username}'`
    const test = await db.query(sql1)
    // console.log(test.data.length);
    // console.log();
    console.log(test.state);
    if (test.state) {
        if (test.data.length !== 0) {
            res.send({
                code: 400,
                msg: '用户名已经存在',
                test
            })
            return
        }
    } else {
        res.send({
            code: 400,
            msg: '请求失败'
        })
        return
    }
    // console.log(test.data);

    let sql = `insert into user (username,password,name) values('${username}','${password}','${username}')`
    const data = await db.query(sql)
    console.log(data);
    if (data.state) {
        res.send({
            code: 200,
            msg: '注册成功',
            data,
        })
    } else {
        res.send({
            code: 400,
            msg: '注册失败',
            data,
        })
    }
})

router.get('/list', async (req, res) => {
    let sql = `select * from user`
    const data = await db.query(sql)
    res.send({
        code: 200,
        msg: '获取成功',
        data
    })
})

// router.get('/login', async (req, res) => {
//     let sql =
// })

router.get('/login', async (req, res) => {
    // console.log(1);
    const { username, password } = req.query

    const tk = jwt.sign(username, 'quexuemovie')
    console.log(tk);
    // console.log(username);
    const sql = `select * from user where username='${username}' and password='${password}';`
    const { state, data } = await db.query(sql)
    if (state) {
        // console.log(state);
        // console.log('data', data[0]);
        if (data.length === 0) {
            res.send({
                code: 400,
                msg: '登陆失败',
                userdata: data[0]
            })
        } else {
            res.send({
                code: 200,
                msg: '登录成功',
                id: data[0].id
            })
        }
    } else {
        res.send({
            code: 400,
            msg: "请求失败"
        })
    }
})

router.delete('/del', async (req, res) => {
    console.log(req);
    const { id } = req.query
    console.log(id);
    const sql = 'delete from user where `id`=' + id
    // console.log(111);
    const { state, data } = await db.query(sql)
    console.log(state);
    if (state) {
        // console.log(11);
        res.send({
            code: 200,
            msg: '删除成功',

        })
    } else {
        res.send({
            code: 400,
            msg: '删除失败'
        })
    }
    // res.send({
    //     code: 200
    // })
})

router.post('/add', async (req, res) => {
    console.log(req.body);
    const { username, password, age, address, tags } = req.body
    // console.log(username);

    // const sql = `inner into user(username,password,age,address,tages,name) values('${username}','${password}','${age}','${address}','${tages}','${username}') `
    // const sql = `insert into user(username,password,age,address,tages,name,key) values('asf','asf','18','asf','asfasfasf','asfasfasf',null) `
    // const sql = `select * from user`
    const sql = `insert into user (username,password,age,tags,name,address)
     values('${username}','${password}','${age}','${tags}','${username}','${address}')`

    const { state, data } = await db.query(sql)

    if (state) {
        res.send({
            code: 200,
            msg: '添加成功'
        })
    } else {
        res.send({
            code: 400,
            msg: '添加失败'
        })
    }
})
